/**
 * @file    wCore_Tree.h
 * @brief   
 *          
 *          
 *
 ****************************************************************************
 * @version $Id: wCore_Tree.h 2460 2010-05-12 07:35:30Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this tree of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this tree of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Gui_Widgets_Core_Tree_H__
 #define __WOSH_Gui_Widgets_Core_Tree_H__

 #include <framework/gui/IWidgetSerializable.h>
 #include <framework/gui/GuiUtilities.h>
 #include <QtGui/QTreeView>
 #include <QAbstractItemModel>

 #include <core/Tree.h>



namespace wosh {
 namespace gui {


// ----------------------------------------------------------------------------------------

class TreeModel_Core_Tree :  public QAbstractItemModel
 {
	Q_OBJECT

	public:
		TreeModel_Core_Tree(QObject *parent = 0);
		virtual ~TreeModel_Core_Tree();

	public:
		WRESULT loadObject( const wosh::Tree* tree );
		WRESULT acquireObject( wosh::Tree* tree );

		inline TreeNode* getRootNode() const {
			if ( this->dataTree == NULL ) return NULL;
			return this->dataTree->getRoot();
		 }

	public:
		int rowCount(const QModelIndex &parent = QModelIndex()) const;
		virtual int columnCount(const QModelIndex &parent = QModelIndex()) const {
			(void)parent; return 1;
		 }

		QModelIndex parent( const QModelIndex &child ) const;
		QModelIndex index( int row, int column, const QModelIndex &parent ) const;

		virtual QVariant data(const QModelIndex &index, int role) const;
		virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;


	public: // sortree
//		void sort( int column, Qt::SortOrder order = Qt::AscendingOrder );

	public: // editree
		 Qt::ItemFlags flags(const QModelIndex &index) const;

//		bool setData(const QModelIndex &index, const QVariant &value, int role);

	signals:
		void treeCell_changed( unsigned int row, unsigned int col, ISerializable* newValue );

	protected:
		wosh::Tree* dataTree;

}; // class def

// ----------------------------------------------------------------------------------------


class wCore_Tree :	public QTreeView,
					public virtual IWidgetSerializableT<wosh::Tree>
 {
	Q_OBJECT

	WOSH_WIDGET_SERIALIZABLE(wosh::gui::wCore_Tree)

	public:
		wCore_Tree(QWidget* parent = NULL);
		virtual ~wCore_Tree();

	public:
		virtual WRESULT loadObject( const wosh::Tree* object );
		virtual WRESULT saveObject( wosh::Tree* object ) const;

	public:
		inline const TreeModel_Core_Tree* getTreeModel() const	{ return this->treeModel; }
		inline TreeModel_Core_Tree* getTreeModel()				{ return this->treeModel; }


	signals:
		void updatedObject();

	protected:
		TreeModel_Core_Tree* treeModel;


}; // class def



// ----------------------------------------------------------------------------------------




 }; // namespace gui
}; // namespace wosh


#endif //__WOSH_Gui_Widgets_Core_Tree_H__
